﻿using System;
using System.Collections.Specialized;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace SetLanguageInfo
{
    public partial class CreateLangInfo : Form
    {
        public CreateLangInfo()
        {
            InitializeComponent();
        }


        private void btnCreateLangInfo_Click(object sender, EventArgs e)
        {
            var langHelper = new LanguageHelper();

            string sourceDirPath = txtCodeDir.Text;
            string targetDirPath = sourceDirPath + LanguageHelper.LANG_CONVERT_KEY_WORD;
            langHelper.CopyCode(sourceDirPath, LanguageHelper.LANG_CONVERT_KEY_WORD);
            string strLangSettingDirPath = targetDirPath + "\\LANG";
            if (!Directory.Exists(strLangSettingDirPath))
                Directory.CreateDirectory(strLangSettingDirPath);
            string strLanguageXmlPath = strLangSettingDirPath + "\\" + LanguageHelper.LANG_KEY + ".xml";
            if (File.Exists(strLanguageXmlPath))
                File.Delete(strLanguageXmlPath);
            var doc = new XmlDocument();
            var fs = new FileStream(strLanguageXmlPath, FileMode.OpenOrCreate);
            var sw = new StreamWriter(fs, Encoding.Default);
            XmlElement settingNode = doc.CreateElement("Setting");
            doc.AppendChild(settingNode);
            try
            {
                doc.Save(sw);
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
            var ndInfo = new NameObjectDictionary();
            var scTargetFileFullNames = new StringCollection();
            langHelper.FileEncoding = Encoding.UTF8;//UTF8编码
            langHelper.GetDirAllFileNamesForSetting(targetDirPath,
                                                    new[] {"*.as?x", "*.cs", "*.js", "*.css", "*.htm", "*.html"},
                                                    scTargetFileFullNames);
            langHelper.SetLanguageInfoFile(doc, scTargetFileFullNames, strLanguageXmlPath, ndInfo);
            scTargetFileFullNames = new StringCollection();
            langHelper.FileEncoding = Encoding.UTF8;
            langHelper.GetDirAllFileNamesForSetting(targetDirPath, new[] {"*.xml"}, scTargetFileFullNames);
            langHelper.SetLanguageInfoFile(doc, scTargetFileFullNames, strLanguageXmlPath, ndInfo);
            langHelper.OutputLangSettingXmlFile(doc, strLanguageXmlPath, ndInfo);
            MessageBox.Show("Create OK");
        }

        private void btnBackUpCode_Click(object sender, EventArgs e)
        {
            string sourceDirPath = txtCodeDir.Text;
            new LanguageHelper().CopyCode(sourceDirPath, "_BackUp[" + DateTime.Now.ToString("yyyy-MM-dd_HHmmss") + "]");
            MessageBox.Show("BackUpCode OK");
        }


        private void btnSelectProDir_Click(object sender, EventArgs e)
        {
            fbdSrcCodeDir.SelectedPath = @"E:\VitCode_Pro";
            if (fbdSrcCodeDir.ShowDialog() == DialogResult.OK)
            {
                txtCodeDir.Text = fbdSrcCodeDir.SelectedPath;
            }
        }

        private void CreateLangVension(string langKey)
        {
            var langHelper = new LanguageHelper();
            string sourceDirPath = txtCodeDir.Text + LanguageHelper.LANG_CONVERT_KEY_WORD;
            string targetDirPath = sourceDirPath + "_" + langKey;
            langHelper.CopyCode(sourceDirPath, "_" + langKey);
            var ndInfo = new NameObjectDictionary();
            string strLanguageXmlPath = targetDirPath + "\\LANG\\" + langKey + ".xml";
            var scTargetFileFullNames = new StringCollection();
            langHelper.FileEncoding = Encoding.UTF8;
            langHelper.GetDirAllFileNamesForSetting(targetDirPath,
                                                    new[]
                                                        {
                                                            "*.as?x", "*.cs", "*.xml", "*.csproj", "*.js", "*.css", "*.htm"
                                                            , "*.html"
                                                        }, scTargetFileFullNames);
            langHelper.SetLanguageInfoFileForCreate(scTargetFileFullNames, strLanguageXmlPath, langKey, ndInfo);
            MessageBox.Show("Create " + langKey + " Vension OK");
        }

        private void TempFix_CreateLangVension(string langKey)
        {
            var langHelper = new LanguageHelper();
            string sourceDirPath = txtCodeDir.Text + LanguageHelper.LANG_CONVERT_KEY_WORD;
            string targetDirPath = sourceDirPath + "_" + langKey;
            langHelper.CopyCode(sourceDirPath, "_" + langKey);
            var ndInfo = new NameObjectDictionary();
            string strLanguageXmlPath = targetDirPath + "\\LANG\\" + langKey + ".xml";
            var scTargetFileFullNames = new StringCollection();
            langHelper.FileEncoding = Encoding.UTF8;
            langHelper.GetDirAllFileNamesForSetting(targetDirPath,
                                                    new[]
                                                        {
                                                            "*.as?x", "*.cs", "*.xml", "*.csproj", "*.js", "*.css", "*.htm"
                                                            , "*.html"
                                                        }, scTargetFileFullNames);
            langHelper.TempFix_SetLanguageInfoFileForCreate(scTargetFileFullNames, strLanguageXmlPath, langKey, ndInfo);
            MessageBox.Show("Create " + langKey + " Vension OK");
        }

        private void btnCreate_CN_Click(object sender, EventArgs e)
        {
            CreateLangVension(LanguageHelper.LANG_CN);
        }

        private void btnCreate_HK_Click(object sender, EventArgs e)
        {
            CreateLangVension(LanguageHelper.LANG_HK);
        }

        private void btnTempFix_Click(object sender, EventArgs e)
        {
            TempFix_CreateLangVension(LanguageHelper.LANG_CN);
        }

        private void btnCompareToCN_Click(object sender, EventArgs e)
        {
            //string sourceDirPath = txtCodeDir.Text + LanguageHelper.LANG_CONVERT_KEY_WORD;
            //string strLanguageXmlPath = targetDirPath + "\\LANG\\" + langKey + ".xml";
        }
    }
}